package com.spark.common.annotation.valid.validator;

import com.spark.common.annotation.valid.Password;
import com.spark.common.constants.RegexConstants;
import org.apache.commons.lang3.StringUtils;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * 密码注解验证器
 *
 * @author LYCHEE
 * @date 2025/2/17 13:41
 */
public class PasswordValidator implements ConstraintValidator<Password, String> {

    @Override
    public void initialize(Password constraintAnnotation) {
        ConstraintValidator.super.initialize(constraintAnnotation);
    }

    @Override
    public boolean isValid(String value, ConstraintValidatorContext context) {
        if (StringUtils.isEmpty(value)){
            return true;
        }
        return value.matches(RegexConstants.PASSWORD_REGEX);
    }
}
